<?php
namespace Admin\Controller;
use Think\Controller;
class CommentController extends CommonController{

/**
 * 评论表
 */
	public function lists(){

		if(IS_GET){

			$comments = M('comments');

			/**
			**数据分页显示开始
			**/
			$count      = $comments->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出

			$list = $comments->query("SELECT fd_comments.*,fd_members.username AS username FROM fd_comments LEFT JOIN fd_members ON fd_comments.mid = fd_members.id ORDER BY id desc LIMIT $Page->firstRow,$Page->listRows");
			/**
			**数据分页结束
			**/
			foreach ($list as $k => $v) {
				# code...
				switch ($v['ctype']) {
					case 1:
						$list[$k]['ctype'] = '项目';
						break;
					case 2:
						$list[$k]['ctype'] = '资讯';
						break;
					case 3:
						$list[$k]['ctype'] = '广告';
						break;
					
					default:
						$list[$k]['ctype'] = '未知';
						break;
				}
			}
			

			$this->assign('list',$list);
			$this->display();
		}
	}


	/**
	 * 隐藏和显示评论功能
	 */
	public function comments_ishidden(){

		if(IS_AJAX){

			$id = I('post.id');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$hidden = I('post.ishidden');
			if(isset($hidden)){

				$ishidden = $hidden;
			}

			$comments = M('comments');

			if($ishidden == 1){

				$data['ishidden'] = 0;
			}else if($ishidden == 0){

				$data['ishidden'] = 1;
			}

			$result = $comments->where($map)->data($data)->save();

			if($result){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}
		}
	}

}